package com.xyc.Untils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUntils {
    private static SqlSessionFactory sqlSessionFactory = null;

    //   1.获取sqlSessionFactory工厂
    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //2. 获取 SqlSession对象
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }

    //   3.测试
    public static void main(String[] args) {
        SqlSession sqlSession = MybatisUntils.getSqlSession();
        System.out.println("sqlSession=" + sqlSession);
    }
}


